unit uServiceLocator_Impl;

interface

uses
  uObjectPool_Intf, uServiceLocator_Intf, uLogManager_Intf, uObjectPool_Impl,
  SysUtils, ADODB, uLogManager_Impl, uDataAccessObjectPoolFactory, uIntfObject_Impl;

type
  TServiceLocator = class;
  TServiceLocator = class(TIntfObject, IServiceLocator)
  private
    FDataAccessObjectPool: TGenericObjectPool;
    FFactory: TBasePoolableObjectFactory;
    FLogManager: TLogManager;
    FLogStorer: TLogStorer;
    function GetDataAccessObjectPool: IObjectPool; stdcall;
    function GetFactory: TBasePoolableObjectFactory;
    function GetLogManager: ILogManager; stdcall;
    function GetLogStorer: TLogStorer;
  protected
    property Factory: TBasePoolableObjectFactory read GetFactory;
    property LogStorer: TLogStorer read GetLogStorer;
  public
    destructor Destroy; override;
    property DataAccessObjectPool: IObjectPool read GetDataAccessObjectPool;
    property LogManager: ILogManager read GetLogManager;
  end;

implementation

{ TServiceLocator }

{
******************************* TServiceLocator ********************************
}
destructor TServiceLocator.Destroy;
begin
  FDataAccessObjectPool:= nil;
  FFactory:= nil;
  FLogStorer:= nil;
  FLogManager:= nil;
  inherited;
end;

function TServiceLocator.GetDataAccessObjectPool: IObjectPool;
begin
  if not Assigned(FDataAccessObjectPool) then
    FDataAccessObjectPool:= TGenericObjectPool.Create(Factory);
  Result:= IObjectPool(FDataAccessObjectPool);
end;

function TServiceLocator.GetFactory: TBasePoolableObjectFactory;
begin
  if not Assigned(FFactory) then
    FFactory:= TADODataAccessObjectPoolFactory.Create;
  Result:= FFactory;
end;

function TServiceLocator.GetLogManager: ILogManager;
begin
  if not Assigned(FLogManager) then
  begin
    FLogManager:= TLogManager.Create(TLog);
    FLogManager.LogStorer:= LogStorer;
    FLogManager.BeLogLevel:= llDebug;
  end;
  Result:= ILogManager(FLogManager);
end;

function TServiceLocator.GetLogStorer: TLogStorer;
begin
  if not Assigned(FLogStorer) then
    FLogStorer:= TTXTLogStorer.Create;
  Result:= FLogStorer;
end;

end.
